<?php
class home_c extends Control
{
	function __construct()
	{
		parent::Control();
		$this->load_model("user");
		$this->load_model("commission");
	}

	function home_c()
	{
		$this->__construct();
	}

	function index_f()
	{
		setcookie("islook",1);
		$urs = $this->user_m->user_from_id($_SESSION["user_id"]);
		$this->tpl->assign("urs",$urs);
		$total = $this->user_m->get_count($_SESSION["user_id"]);
		$money = $this->user_m->get_money($_SESSION["user_id"]);
		if(!$money){
			$money ='0';
		}
		$this->tpl->assign("total",$total);
		$this->tpl->assign("money",$money);
		$this->tpl->display("home.".$this->tpl->ext);
	}
	
	function my_f()
	{
		
		//判断用户UA是否为微信
		$user_agent = $_SERVER['HTTP_USER_AGENT'];
		if (strpos($user_agent, 'MicroMessenger')) {
			if(empty($_SESSION['user_id'])){
				$appid='wxa9adc2cde1638e75';
				$secret = "ac5418ff04ffcc3e0bb05c38bff32821"; 
				$ac = $_GET["ac"];
				if(empty($ac)){
					$REDIRECT_URI='http://'.$_SERVER["SERVER_NAME"].'/index.php?c=home&f=my&ac=uinfo';
					$scope='snsapi_base';//需要授权
					$url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.urlencode($REDIRECT_URI).'&response_type=code&scope='.$scope.'&state='.$state.'#wechat_redirect';
					header("Location:".$url);
					exit();
				}else if($ac=="uinfo"){
					$code = $_GET["code"]; 
					$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
					$ch = curl_init();
					curl_setopt($ch,CURLOPT_URL,$get_token_url);
					curl_setopt($ch,CURLOPT_HEADER,0); 
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); 
					curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 
					$res = curl_exec($ch); 
					curl_close($ch); 
					$json_obj = json_decode($res,true);
					//根据openid和access_token查询用户信息 
					$access_token = $json_obj['access_token']; 
					$openid = $json_obj['openid'];
					
					//检查会员不存在时的警告
					$rs = $this->user_m->user_from_openid($openid);
					if($rs)
					{
						//检查会员状态的警告
						if(!$rs["status"])
						{
							echo $this->lang["login_false_check"];
							exit;
						}
						//检查会员是否被锁定
						if($rs["status"] == 2)
						{
							echo $this->lang["login_false_check"];
							exit;
						}
						//将数据存到session中
						if($rs["status"] == 1)
						{
							
							$refer = 'http://'.$_SERVER["SERVER_NAME"].'/index.php?c=home&f=my';
							$_SESSION["user_id"] = $rs["id"];
							$_SESSION["user_name"] = $rs["username"];
							$_SESSION["group_id"] = $rs["groupid"];
							$_SESSION["user_rs"]= $rs;
							$_SESSION["headimgurl"] = $rs["headimgurl"];
							header("Location:".$refer);
							exit();
						}
					}
				}
			}
		}
		
		
		$urs = $this->user_m->user_from_id($_SESSION["user_id"]);
		$this->tpl->assign("urs",$urs);
		$total = $this->user_m->get_count($_SESSION["user_id"]);
		$money = $this->user_m->get_money($_SESSION["user_id"]);
		if(!$money){
			$money ='0';
		}
		$this->tpl->assign("total",$total);
		$this->tpl->assign("money",$money);
		$this->tpl->display("index.".$this->tpl->ext);
	}
	
	
	function updategroup_f()
	{
		if(!$_SESSION["user_id"])
		{
			echo "分享成功！";
			exit;
		}else{
			$uid = $_SESSION["user_id"];
			
			$urs = $this->user_m->user_from_id($_SESSION["user_id"]);
			if($urs['fxstatus']!=1){
				//发放奖励
				$array["uid"] = $uid;
				$array["uname"] = $urs["username"];
				$array["cid"] = '0';
				$array["username"] ="分享奖励";
				$array["proname"] = "达人传媒";
				$array["ctype"] = "激励";
				$array["money"] = 20;
				$array["postdate"] = $this->system_time;
				$this->commission_m->save($array,$id);
				
				$this->user_m->update_group($uid);
				echo "分享成功，你已经升级为金牌经纪人并获得奖励佣金！";
				exit;
			}else{
				echo "分享成功！";
				exit;
			}
		}
	}
}
?>